#include "smTexture3D.h"
#include "smError.h" 

namespace ShaderManager
{

smTexture3D::smTexture3D(GLuint texture_unit)
: smTexture(texture_unit)
{

}

void smTexture3D::bind()
{
	glBindTexture(GL_TEXTURE_3D,id);
}

void smTexture3D::unbind()
{
	glBindTexture(GL_TEXTURE_3D,0);
}

void smTexture3D::load(int size, void *data, GLuint internal_format, GLuint format, GLuint type, bool linear, bool clamp)
{
	int size3[3];
	size3[0] = size;
	size3[1] = size;
	size3[2] = size;
	bool clamp3[3];
	clamp3[0] = clamp;
	clamp3[1] = clamp;
	clamp3[2] = clamp;
	load(size3, data, internal_format, format, type, linear, clamp3);
}
void smTexture3D::load(int size[3], void *data, GLuint internal_format, GLuint format, GLuint type, bool linear, bool clamp)
{
	bool clamp3[3];
	clamp3[0] = clamp;
	clamp3[1] = clamp;
	clamp3[2] = clamp;
	load(size, data, internal_format, format, type, linear, clamp3);
}
void smTexture3D::load(int size, void *data, GLuint internal_format, GLuint format, GLuint type, bool linear, bool clamp[3])
{
	int size3[3];
	size3[0] = size;
	size3[1] = size;
	size3[2] = size;
	load(size3, data, internal_format, format, type, linear, clamp);
}

void smTexture3D::load(int size[3], void *data, GLuint internal_format, GLuint format, GLuint type, bool linear, bool clamp[3])
{
	active();
	bind();

	glTexImage3D(
			GL_TEXTURE_3D,
			0, //Mipmap : aucun
			internal_format, //GL_RGB16
			size[0], size[1], size[2],
			0, //Largeur du bord : 0
			format, //GL_RGB,
			type,
			data);

	if(clamp[0])
	{
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE  );
	}
	else
	{
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT );
	}

	if(clamp[1])
	{
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE  );
	}
	else
	{
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT );
	}

	if(clamp[2])
	{
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE  );
	}
	else
	{
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT );
	}

	if(linear)
	{
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	}
	else
	{
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	}


	CheckGLError("Texture3D::load");
}

}